﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public delegate void PagingBindControl(object sender, PagingEventArgs e);

public class PagingEventArgs : EventArgs
{
    public PagingEventArgs(int pageIndex)
    {
        this.pageIndex = pageIndex;
    }
    public int pageIndex;
}

public partial class UserControl_PagingCtrl : System.Web.UI.UserControl
{
    public event PagingBindControl PageIndexChanged;
    private int _totalRows;
    private int _totalPages;
    private int _pageSize;
    private int _currentPageNO;
    private int _firstPageNO;
    private int _lastPageNO;
    private int _showPageMinNumber;
    private int _showPageMaxNumber;
    private int _pageRange;

    public int PageRange
    {
        get { return _pageRange; }
        set {
            if (value <= 0)
            {
                _pageRange = 10;
            }
            else
            {
                _pageRange = value;
            }
        }
    }
	

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.PreRender += new EventHandler(UserControl_PagingCtrl_PreRender);
        }
    }

    void UserControl_PagingCtrl_PreRender(object sender, EventArgs e)
    {
        this.RenderPc();
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        if (this.PageIndexChanged != null)
        {
            PageIndexChanged(sender, new PagingEventArgs(Convert.ToInt32(lb.CommandName)));
        }
        this.RenderPc();
    }

    public void InitPc(int totalrows, int pageno, int pagesize)
    {
        this._totalRows = totalrows;
        this._pageSize = pagesize;
        this._totalPages = (totalrows - 1) / pagesize + 1; //取得最大页数
        this._firstPageNO = 1;
        this._lastPageNO = this._totalPages;
        this._currentPageNO = ValidPageNO(pageno);
        //this._pageRange = 10;
        CalculateShowPageNumber();
        //RenderPc();
    }

    private int ValidPageNO(int pageno)
    {
        return (pageno < this._lastPageNO ?
                (pageno > this._firstPageNO ? pageno :
                 this._firstPageNO) :
                this._lastPageNO);
    }

    protected void RenderPc()
    {
        this.labNoncePage.Text = "当前第" + this._currentPageNO.ToString() + "页";
        this.labPageAmountt.Text = "共" + this._totalPages.ToString() + "页";

        this.PageNumber();

        this.btnFirst.CommandName = this._firstPageNO.ToString();
        this.btnLast.CommandName = this._lastPageNO.ToString();
        //int p = 0;
        //int n = 0;
        //if ((this._currentPageNO - 1) < this._firstPageNO)
        //{
        //    p = this._firstPageNO;
        //}
        //else
        //{
        //    p = this._currentPageNO - 1;
        //}
        //if ((this._currentPageNO + 1) > this._lastPageNO)
        //{
        //    n = this._lastPageNO;
        //}
        //else
        //{
        //    n = this._currentPageNO + 1;
        //}
        //this.btnPrevious.CommandName = p.ToString();
        //this.btnNext.CommandName = n.ToString();
        this.btnPrevious.CommandName = Convert.ToString((this._currentPageNO - 1) < this._firstPageNO ? this._firstPageNO : (this._currentPageNO - 1));
        this.btnNext.CommandName = Convert.ToString((this._currentPageNO + 1) > this._totalPages ? this._totalPages : (this._currentPageNO + 1));
    }

    private void CalculateShowPageNumber()
    {
        if (this._totalPages <= this._pageRange)
        {
            this._showPageMinNumber = 1;
            this._showPageMaxNumber = this._totalPages;
        }
        else
        {
            if ((this._totalPages / this._pageRange) == ((this._currentPageNO - 1) / this._pageRange))
            {
                this._showPageMinNumber = (this._currentPageNO / this._pageRange) * this._pageRange + 1;
                this._showPageMaxNumber = this._totalPages;
            }
            else
            {
                this._showPageMinNumber = ((this._currentPageNO - 1) / this._pageRange) * this._pageRange + 1;
                this._showPageMaxNumber = ((this._currentPageNO - 1) / this._pageRange + 1) * this._pageRange;
            }
        }
    }

    private void PageNumber()
    {
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("PageCount", typeof(int));
        dt.Columns.Add(dc);
        for (int i = _showPageMinNumber; i <= _showPageMaxNumber; i++)
        {
            DataRow dr = dt.NewRow();
            dr["PageCount"] = i;
            dt.Rows.Add(dr);
        }
        this.datPageNumber.DataSource = dt;
        this.datPageNumber.DataBind();
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        int page = 1;
        try
        {
			
            page = Convert.ToInt32(this.txtPageCount.Value);
		this._totalPages=Convert.ToInt32(this.btnLast.CommandName);
            if (page <= 0) {
                page = 1;
            }
            if (page > this._totalPages) {
                page = this._totalPages;
            }


		if (this.PageIndexChanged != null)
        	{
          		  PageIndexChanged(sender, new PagingEventArgs(page));
       		 }
       		 this.RenderPc();
      	  }
        catch 
        {
        }
       
    }
}
